package tool;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.mengtian_tech.smart_lab.entity.apply.ApplyForm;
import com.mengtian_tech.smart_lab.entity.cms.Attach;

/**
 * 利用开源组件POI3.0.2动态导出EXCEL文档 转载时请保留以下信息，注明出处！
 * 
 * @author leno
 * @version v1.0
 * @param <T>
 *            应用泛型，代表任意一个符合javabean风格的类
 *            注意这里为了简单起见，boolean型的属性xxx的get器方式为getXxx(),而不是isXxx()
 *            byte[]表jpg格式的图片数据
 */
public class ExportExcel
{
	public void exportExcel(ApplyForm applyForm, OutputStream out)
	{
		exportExcel("测试POI导出EXCEL文档"+System.currentTimeMillis(), 
				new String[]{"日期","2017/07-2017/09"},
				applyForm, out, "yyyy-MM-dd");
	}

	public File exportExcelToTempFile(ApplyForm applyForm,String filename) throws Exception
	{
		
		File file = File.createTempFile(filename+"-"+System.currentTimeMillis()+"", ".xls");
		exportExcel("测试POI导出EXCEL文档"+System.currentTimeMillis(), 
				new String[]{"日期","2017/07-2017/09"},
				applyForm, new FileOutputStream(file), "yyyy-MM-dd");
		return file;
	}

 
	/**
	 * 这是一个通用的方法，利用了JAVA的反射机制，可以将放置在JAVA集合中并且符号一定条件的数据以EXCEL 的形式输出到指定IO设备上
	 * 
	 * @param title
	 *            表格标题名
	 * @param headers
	 *            表格属性列名数组
	 * @param dataset
	 *            需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。此方法支持的
	 *            javabean属性的数据类型有基本数据类型及String,Date,byte[](图片数据)
	 * @param out
	 *            与输出设备关联的流对象，可以将EXCEL文档导出到本地文件或者网络中
	 * @param pattern
	 *            如果有时间数据，设定输出格式。默认为"yyy-MM-dd"
	 */
	@SuppressWarnings("unchecked")
	public void exportExcel(String title, String[] headers,
			ApplyForm applyForm, OutputStream out, String pattern)
	{
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = workbook.createSheet(title);
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth((short) 15);
		// 生成一个样式
		HSSFCellStyle style = workbook.createCellStyle();
		// 设置这些样式
		style.setFillForegroundColor(HSSFColor.WHITE.index);
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 生成一个字体
		HSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.BLACK.index);
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		style.setFont(font);
		// 生成并设置另一个样式
		HSSFCellStyle style2 = workbook.createCellStyle();
		style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 生成另一个字体
		HSSFFont font2 = workbook.createFont();
		font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		// 把字体应用到当前的样式
		style2.setFont(font2);

		// 声明一个画图的顶级管理器
		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
		 
		//产生表格标题行
		HSSFRow row = sheet.createRow(0);
		for (short i = 0; i < headers.length; i++)
		{
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(headers[i]);
			cell.setCellValue(text);
		}
		
		//产生表格标题行2
		String subHeaders[] = new String[]{
				"","数额","增值税","净额","备注"
		};
		row = sheet.createRow(1);
		for (short i = 0; i < subHeaders.length; i++)
		{
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(subHeaders[i]);
			cell.setCellValue(text);
		}
		
		//有一行
		subHeaders = new String[]{
				"销售额",applyForm.getSellAmount().toString(),applyForm.getSellAmountTax().toString(),
				applyForm.getSellAmount().subtract(applyForm.getSellAmountTax()).toString(),""
		};
		row = sheet.createRow(2);
		for (short i = 0; i < subHeaders.length; i++)
		{
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(subHeaders[i]);
			cell.setCellValue(text);
		}
		
		//第四行
		subHeaders = new String[]{
				"开支",applyForm.getSpendAmount().toString(),applyForm.getSpendAmountTax().toString(),
				applyForm.getSpendAmount().subtract(applyForm.getSpendAmountTax()).toString(),""
		};
		row = sheet.createRow(3);
		for (short i = 0; i < subHeaders.length; i++)
		{
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(subHeaders[i]);
			cell.setCellValue(text);
		}
		
		//第五行
		BigDecimal balanceTax = applyForm.getBalance().divide(new BigDecimal(11),RoundingMode.HALF_DOWN);
		subHeaders = new String[]{
				"利润",applyForm.getBalance().toString(),
				balanceTax.toString(),
				applyForm.getBalance().subtract(balanceTax).toString(),""
		};
		row = sheet.createRow(4);
		for (short i = 0; i < subHeaders.length; i++)
		{
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(subHeaders[i]);
			cell.setCellValue(text);
		}

		//第六行
		row = sheet.createRow(5);
		HSSFCell cell = row.createCell(0);
		cell.setCellStyle(style);
		HSSFRichTextString text = new HSSFRichTextString("签名");
		cell.setCellValue(text);
		
		//写入签名图片
		byte[] data = applyForm.getAttachData();
		if(data != null){
			//有图片时，设置行高为60px;
			row.setHeightInPoints(60);
			// 设置图片所在列宽度为80px,注意这里单位的一个换算
			sheet.setColumnWidth(1, (short) (35.7 * 160));
							// sheet.autoSizeColumn(i);
			HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,
					1023, 255, (short) 1, 5, (short) 1, 5);
			anchor.setAnchorType(2);
			patriarch.createPicture(anchor, workbook.addPicture(
									data, HSSFWorkbook.PICTURE_TYPE_JPEG));
		}
					 
		try
		{
			workbook.write(out);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	} 
}

